import com.sun.jdi.Value;
import log.AsyncManger;
import log.LogTask;
import log.LoginLog;
import log.RunLog;

import java.security.Key;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class Functions {
    //  ArrayList<User> users = new ArrayList<>();
    private static String managerName = "admin";
    private static String managerPassword = "1234";
    private static ArrayList<ClassRoom> classes = new ArrayList<>();
    private static Set<Student> students = new HashSet<>();
    private static ArrayList<Department> departments = new ArrayList<>();
    private static Set<Employee> employees = new HashSet<>();
    private static RunLog runLog = new RunLog();
    private static LoginLog loginLog = new LoginLog();
    private static String loginUser;

    public Functions() {
    }

    public static void login() throws Exception {
        // users.add(new User("admin", "1234"));
        Scanner scanner = new Scanner(System.in);
        System.out.println("==欢迎进入tlias智能学习辅助系统==");
        int loginCount = 3;
        while (loginCount >= 0) {
            System.out.println("请输入管理员账户：");
            String username = scanner.nextLine();
            System.out.println("请输入管理员密码：");
            String password = scanner.nextLine();
            if (username.equals(managerName) && password.equals(managerPassword)) {
                System.out.println("登陆成功");
                loginUser = username;
                AsyncManger.getInstance().execute(new LogTask(loginLog, loginUser, "登录"));
                console();
                return;
            }
            System.out.println("用户名或密码错误,请重新输入，你还有" + (loginCount--) + "次机会！");
        }
    }

    public static void console() throws Exception {
       /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("2024-02-04");
        Date date2 = sdf.parse("2024-02-05");
        Date date3 = sdf.parse("2024-02-06");
        Date date4 = sdf.parse("2024-02-07");

        classes.add(new ClassRoom("一班", 101, date, date2, "张三"));
        classes.add(new ClassRoom("二班", 102, date3, date4, "李四"));
        students.add(new Student("学生1", "A101", "男", "13694394093", "大专", "一班"));
        students.add(new Student("学生2", "A102", "女", "13694394327", "大学本科", "二班"));
        departments.add(new Department("学工部", "老师"));
        departments.add(new Department("技术部", "专工"));
        employees.add(new Employee("user1", "员工1", "男", "老师", date3, "学工部"));
        employees.add(new Employee("user2", "员工2", "女", "专工", date4, "技术部"));*/
        DatabaseConnection();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1.班级学院管理");
            System.out.println("2.系统信息管理");
            System.out.println("3.数据统计管理");
            System.out.println("4.修改密码");
            System.out.println("5.退出登录");
            System.out.println("请输入你的选择：");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }

            switch (choice) {
                case 1:
                    classUserManage();
                    break;
                case 2:
                    systemInfoManage();
                    break;
                case 3:
                    employeStatistics();
                    break;
                case 4:
                    modifyPassword();
                    return;
                case 5:
                    System.out.println("欢迎您的使用！退出程序");
                    AsyncManger.getInstance().execute(new LogTask(loginLog, loginUser, "退出"));
                    DatabaseOutPut();
                    return;
                default:
                    System.out.println("没有该选项！请重新输入！");
            }
        }

    }

    public static void modifyPassword() throws Exception {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入原密码！");
            String oldPassword = scanner.nextLine();
            if (!oldPassword.equals(managerPassword)) {
                System.out.println("原密码错误！");
                continue;
            }
            System.out.println("请输入新密码！");
            String newPassword = scanner.nextLine();
            if (oldPassword.equals(newPassword)) {
                System.out.println("新密码和原密码一致！");
            } else {
                System.out.println("请再次输入新密码！");
                String newPassword2 = scanner.nextLine();
                if (newPassword.equals(newPassword2)) {
                    managerPassword = newPassword;
                    System.out.println("修改密码成功，回到登录界面");
                    login();
                    return;
                } else {
                    System.out.println("两次输入的新密码不一致，请重新输入！");
                }
            }
        }
    }

    public static void classUserManage() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==班级学员管理==");
            System.out.println("1.班级管理");
            System.out.println("2.学员管理");
            System.out.println("3.回到主界面");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    classManager();
                    break;
                case 2:
                    studentManager();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("没有这个选项，请重新输入！");
            }
        }
    }

    public static void classManager() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==班级管理==");
            System.out.println("1.新增班级");
            System.out.println("2.修改班级");
            System.out.println("3.删除班级");
            System.out.println("4.查看班级");
            System.out.println("5.返回上一级菜单");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    addClass();
                    break;
                case 2:
                    modifyClass();
                    break;
                case 3:
                    deleteClass();
                    break;
                case 4:
                    viewClass();
                    break;
                case 5:
                    System.out.println("返回中。。。");
                    return;
                default:
                    System.out.println("没有这个选项，请重新输入！");
            }
        }
    }

    public static void addClass() {
        Scanner scanner = new Scanner(System.in);
        String className = null;
        int classId = 0;
        Date startDate = null;
        Date endDate = null;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        while (true) {
            System.out.println("输入新班级名称：");
            className = scanner.nextLine();
            Boolean isRepeat = false;
            for (int i = 0; i < classes.size(); i++) {
                if (className.equals(classes.get(i).getClassName())) {
                    System.out.println("班级名称重复！");
                    isRepeat = true;
                }
            }
            if (!isRepeat) {
                break;
            }
        }
        while (true) {
            System.out.println("输入新班级教室：");
            classId = scanner.nextInt();
            scanner.nextLine();
            boolean isrepeat = false;
            for (int i = 0; i < classes.size(); i++) {
                if (classId == (classes.get(i).getClassRoom())) {
                    System.out.println("该教室已被其他班级占用，请重新选择！");
                    isrepeat = true;
                }
            }
            if (!isrepeat) {
                break;
            }
        }

        while (true) {
            System.out.println("输入新班级开课时间,中间用'-'表示");
            String start = scanner.nextLine();
            try {
                startDate = sdf.parse(start);
                break;
            } catch (ParseException e) {
                System.out.println("日期格式不对！请重新输入！");
            }
        }
        while (true) {
            System.out.println("输入新班级结课时间,中间用'-'表示");
            String end = scanner.nextLine();
            try {
                endDate = sdf.parse(end);
                break;
            } catch (ParseException e) {
                System.out.println("日期格式不对！请重新输入！");
            }
        }
        System.out.println("输入新班级班主任！");
        String headTeacher = scanner.nextLine();
        ClassRoom newClass = new ClassRoom(className, classId, startDate, endDate, headTeacher);
        classes.add(newClass);
        System.out.println("班级添加成功");
        AsyncManger.getInstance().execute(new LogTask(runLog, loginUser, "新增班级"));
    }

    public static void modifyClass() {
        Scanner scanner = new Scanner(System.in);
        String className = null;
        int classId = 0;
        String start = null;
        String end = null;
        Date startDate = null;
        Date endDate = null;
        String headTeacher = null;
        int oldClassNum = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("输入修改的班级名称：");
        className = scanner.nextLine();


        boolean exist = false;
        for (int i = 0; i < classes.size(); i++) {
            if (className.equals(classes.get(i).getClassName())) {
                oldClassNum = i;
                exist = true;
            }
        }
        if (!exist) {
            System.out.println(className + " 班级名称不存在");
            return;
        }
        while (true) {
            System.out.println("输入修改后的班级教室：");
            classId = scanner.nextInt();
            scanner.nextLine();
            Boolean classRepeat = false;
            for (int i = 0; i < classes.size(); i++) {
                if ((!className.equals(classes.get(i).getClassName()) && (classId == (classes.get(i).getClassRoom())))) {
                    System.out.println("该教室已被其他班级占用，请重新选择！");
                    classRepeat = true;
                }

            }
            if (!classRepeat) {
                break;
            }
        }
        while (true) {
            System.out.println("输入修改后的班级开课时间,中间用'-'表示");
            start = scanner.nextLine();
            try {
                startDate = sdf.parse(start);
                break;
            } catch (ParseException e) {
                System.out.println("日期格式不对！请重新输入！");
            }
        }
        while (true) {
            System.out.println("输入修改后的班级结课时间,中间用'-'表示");
            end = scanner.nextLine();
            try {
                endDate = sdf.parse(end);
                break;
            } catch (ParseException e) {
                System.out.println("日期格式不对！请重新输入！");
            }
        }
        System.out.println("输入修改后的班级班主任：");
        headTeacher = scanner.nextLine();
        ClassRoom newClass = new ClassRoom(className, classId, startDate, endDate, headTeacher);
        classes.set(oldClassNum, newClass);
        System.out.println("修改成功");
        AsyncManger.getInstance().execute(new LogTask(runLog, loginUser, "修改班级"));
    }

    public static void deleteClass() {
        Scanner scanner = new Scanner(System.in);
        int input = 0;
        while (true) {
            System.out.println("请输入删除的班级名称：");
            String className = scanner.nextLine();
            int classNum = 0;
            boolean exist = false;
            for (int i = 0; i < classes.size(); i++) {
                if (classes.get(i).getClassName().equals(className)) {
                    classNum = i;
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("不存在该班级名称！");
                return;
            }
            exist = false;
            for (Student student : students) {
                if (student.getStudentClass().equals(classes.get(classNum).getClassName())) {
                    exist = true;
                }
            }
            if (exist) {
                System.out.println("该班级下存在学生，无法删除！");
                return;
            }
            while (true) {
                System.out.println("确认删除班级" + className + "吗？1-确定 2-取消");
                try {
                    input = scanner.nextInt();
                } catch (Exception e) {
                    System.out.println("输入格式错误！");
                    continue;
                }
                if (input == 1) {
                    classes.remove(classNum);
                    System.out.println(className + " 删除成功。");
                    AsyncManger.getInstance().execute(new LogTask(runLog, loginUser, "删除班级"));
                    return;
                } else if (input == 2) {
                    System.out.println("取消删除。");
                    return;
                } else {
                    System.out.println("输入错误！请重新输入！");
                }
            }

        }
    }

    public static void viewClass() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1.查看所有班级");
            System.out.println("2.班级名称查询");
            System.out.println("3.结课时间查询");
            System.out.println("4.返回上一级菜单");
            System.out.println("请输入您的选择：");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    viewAllClass();
                    break;
                case 2:
                    searchNameClass();
                    break;
                case 3:
                    serachDateClass();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("没有此选项，请重新输入！");

            }
        }
    }

    public static void viewAllClass() {
        // 定义列宽
        int classNameWidth = 10;
        int classRoomWidth = 8;
        int startDateWidth = 12;
        int endDateWidth = 12;
        int headTeacherWidth = 15;

        // 格式化输出表头
        System.out.printf("%-" + classNameWidth + "s | %-" + classRoomWidth + "s | %-" + startDateWidth + "s | %-" + endDateWidth + "s | %-" + headTeacherWidth + "s%n",
                "班级名称", "教室编号", "开课时间", "结课时间", "班主任");


        // 格式化输出数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (ClassRoom cls : classes) {
            System.out.printf("%-" + classNameWidth + "s | %-" + classRoomWidth + "d | %-" + startDateWidth + "s | %-" + endDateWidth + "s | %-" + headTeacherWidth + "s%n",
                    cls.getClassName(), cls.getClassRoom(), sdf.format(cls.getStartDate()), sdf.format(cls.getEndDate()), cls.getHeadTeacher());
        }

    }

    public static void searchNameClass() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入班级名称");
        String className = scanner.nextLine();
        // 定义列宽
        int classNameWidth = 10;
        int classRoomWidth = 8;
        int startDateWidth = 12;
        int endDateWidth = 12;
        int headTeacherWidth = 15;

        // 格式化输出表头
        System.out.printf("%-" + classNameWidth + "s | %-" + classRoomWidth + "s | %-" + startDateWidth + "s | %-" + endDateWidth + "s | %-" + headTeacherWidth + "s%n",
                "班级名称", "教室编号", "开课时间", "结课时间", "班主任");


        // 格式化输出数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            for (ClassRoom cls : classes) {
                if (cls.getClassName().equals(className)) {
                    System.out.printf("%-" + classNameWidth + "s | %-" + classRoomWidth + "d | %-" + startDateWidth + "s | %-" + endDateWidth + "s | %-" + headTeacherWidth + "s%n",
                            cls.getClassName(), cls.getClassRoom(), sdf.format(cls.getStartDate()), sdf.format(cls.getEndDate()), cls.getHeadTeacher());
                }
            }
        } catch (Exception e) {
            System.out.println("日期格式错误！");
        }
    }

    public static void serachDateClass() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入结课时间");
        String endDate = scanner.nextLine();
        // 定义列宽
        int classNameWidth = 10;
        int classRoomWidth = 8;
        int startDateWidth = 12;
        int endDateWidth = 12;
        int headTeacherWidth = 15;

        // 格式化输出表头
        System.out.printf("%-" + classNameWidth + "s | %-" + classRoomWidth + "s | %-" + startDateWidth + "s | %-" + endDateWidth + "s | %-" + headTeacherWidth + "s%n",
                "班级名称", "教室编号", "开课时间", "结课时间", "班主任");


        // 格式化输出数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            for (ClassRoom cls : classes) {
                if (cls.getEndDate().equals(sdf.parse(endDate))) {
                    System.out.printf("%-" + classNameWidth + "s | %-" + classRoomWidth + "d | %-" + startDateWidth + "s | %-" + endDateWidth + "s | %-" + headTeacherWidth + "s%n",
                            cls.getClassName(), cls.getClassRoom(), sdf.format(cls.getStartDate()), sdf.format(cls.getEndDate()), cls.getHeadTeacher());
                }
            }
        } catch (ParseException e) {
            System.out.println("日期格式错误！");
            ;
        }
    }

    public static void studentManager() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("=学员管理=");
            System.out.println("1.新增学员");
            System.out.println("2.修改学员");
            System.out.println("3.删除学员");
            System.out.println("4.查看学员");
            System.out.println("5.违纪扣分");
            System.out.println("6.返回上一级菜单");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    addStudents();
                    break;
                case 2:
                    modifyStudents();
                    break;
                case 3:
                    deleteStudents();
                    break;
                case 4:
                    viewStudents();
                    break;
                case 5:
                    deductPoints();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("没有该选项，请重新输入！");
            }
        }
    }

    public static void addStudents() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        while (true) {
            System.out.println("请输入新增学员姓名");
            String studentName = scanner.nextLine();
            System.out.println("请输入新增学员学号");
            String studentID = scanner.nextLine();

            for (Student student : students) {
                if (student.getStudentID().equals(studentID)) {
                    System.out.println("学号重复，请重新输入！");
                    exist = true;
                }
            }
            if (exist) {
                continue;
            }
            System.out.println("请输入新增学员性别");
            String studentGender = scanner.nextLine();
            System.out.println("请输入新增学员手机号");
            String studentPhone = scanner.nextLine();
            System.out.println("请输入新增学员学历");
            String studentEdu = scanner.nextLine();
            while (true) {
                System.out.println("请输入新增学员所属班级");
                System.out.println("现有班级如下：");
                for (ClassRoom cls : classes) {
                    System.out.println(cls.getClassName());
                }
                exist = false;
                String studentClass = scanner.nextLine();
                for (ClassRoom cls : classes) {
                    if (cls.getClassName().equals(studentClass)) {
                        exist = true;
                    }
                }
                if (!exist) {
                    System.out.println("没有此班级！请重新选择！");
                    continue;
                }
                Student student = new Student(studentName, studentID, studentGender, studentPhone, studentEdu, studentClass);
                students.add(student);
                break;
            }
        }
    }

    public static void modifyStudents() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;

        while (true) {

            System.out.println("请输入想要修改的学生学号");
            String studentID = scanner.nextLine();

            for (Student student : students) {
                if (student.getStudentID().equals(studentID)) {
                    exist = true;

                }
            }
            if (!exist) {
                System.out.println("学号不存在，不可修改！");
                continue;
            }
            System.out.println("请输入学员姓名");
            String studentName = scanner.nextLine();
            System.out.println("请输入学员性别");
            String studentGender = scanner.nextLine();
            System.out.println("请输入学员手机号");
            String studentPhone = scanner.nextLine();
            System.out.println("请输入学员最高学历");
            String studentEdu = scanner.nextLine();
            System.out.println("请输入学员所属班级");
            System.out.println("现有班级如下：");
            for (ClassRoom cls : classes) {
                System.out.println(cls.getClassName());
            }
            exist = false;
            String studentClass = scanner.nextLine();
            for (ClassRoom cls : classes) {
                if (cls.getClassName().equals(studentClass)) {
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("没有此班级！请重新选择！");
                continue;
            }
            Student newStudent = new Student(studentID, studentName, studentGender, studentPhone, studentEdu, studentClass);
            for (Student student : students) {
                if (student.getStudentID().equals(studentID)) {
                    students.remove(student);
                    students.add(newStudent);
                }
            }
            break;
        }
    }

    public static void deleteStudents() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择删除方式:1-根据学号单个删除 2-根据学号批量删除");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    deleteStudentsSingle();
                    return;
                case 2:
                    deleteStudentsMultiple();
                    return;
                default:
                    System.out.println("没有该选项，请重新输入！");
            }
        }
    }

    public static void deleteStudentsSingle() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入想要删除的学员学号：");
            boolean exist = false;
            for (Student student : students) {
                if (student.getStudentID().equals(student.getStudentID())) {
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("学号不存在！请重新输入！");
                continue;
            }
            break;
        }
        String studentID = scanner.nextLine();
        for (Student student : students) {
            if (student.getStudentID().equals(studentID)) {
                while (true) {
                    System.out.println("请确认是否删除：1-确定 2-取消");
                    int choice = 0;
                    try {
                        choice = scanner.nextInt();
                    } catch (Exception e) {
                        System.out.println("输入类型错误，请重新输入！");
                        scanner.next();
                        continue;
                    }
                    switch (choice) {
                        case 1:
                            students.remove(student);
                            System.out.println(student.getStudentName() + "（" + student.getStudentID() + "），删除成功");
                            return;
                        case 2:
                            System.out.println("取消删除");
                            return;
                        default:
                            System.out.println("没有该选项！请重新输入！");
                    }

                }
            }
        }

    }

    public static void deleteStudentsMultiple() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("批量删除，请输入多个学员的学号，之间用逗号','隔开");
        String studentID = scanner.nextLine();
        String[] studentsID = studentID.split(",");
        for (Student student : students) {
            boolean exist = false;
            for (String studentName : studentsID) {
                if (student.getStudentID().equals(studentName)) {
                    exist = true;
                    students.remove(student);
                    System.out.println("学号" + studentName + "删除成功");
                }
            }
            if (!exist) {
                System.out.println("学号" + student.getStudentID() + "不存在");
            }
        }
    }

    public static void viewStudents() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1.查看所有学员");
            System.out.println("2.学员名称查询");
            System.out.println("3.学员学号查询");
            System.out.println("4.学历查询");
            System.out.println("5.所属班级查询");
            System.out.println("6.返回上一级菜单");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    ListAllStudents();
                    break;
                case 2:
                    studentNameSearch();
                    break;
                case 3:
                    studentIDSearch();
                    break;
                case 4:
                    studentEduSearch();
                    break;
                case 5:
                    studentClassSearch();
                    break;
                case 6:
                    return;
            }
        }
    }

    public static void ListAllStudents() {
        System.out.println(String.format("%-10s | %-10s | %-10s | %-15s | %-10s | %-10s | %-10s | %-10s",
                "姓名", "学号", "性别", "电话号码", "学历", "班级", "违规次数", "违规分数"));

        // 打印分割线
        System.out.println("-------------------------------------------------------------------------------------------------");

        // 打印学生信息
        int index = 1;
        for (Student student : students) {
            System.out.println(String.format("%-5d | %s", index++, student.toString()));
        }
    }

    public static void studentNameSearch() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查询的学生姓名");
        String studentName = scanner.nextLine();
        boolean exist = false;
        int index = 1;
        for (Student student : students) {
            if (student.getStudentName().equals(studentName)) {
                System.out.println(String.format("%-5d | %s", index++, student.toString()));
                exist = true;
            }
        }
        if (!exist) {
            System.out.println("不存在该学生！");
        }
    }

    public static void studentIDSearch() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查询的学生学号");
        String studentID = scanner.nextLine();
        boolean exist = false;
        int index = 1;
        for (Student student : students) {
            if (student.getStudentID().equals(studentID)) {
                System.out.println(String.format("%-5d | %s", index++, student.toString()));
                exist = true;
            }
        }
        if (!exist) {
            System.out.println("不存在该学生！");
        }
    }

    public static void studentEduSearch() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查询的学生学历");
        String studentEdu = scanner.nextLine();
        boolean exist = false;
        int index = 1;
        for (Student student : students) {
            if (student.getStudentEdu().equals(studentEdu)) {
                System.out.println(String.format("%-5d | %s", index++, student.toString()));
                exist = true;
            }
        }
        if (!exist) {
            System.out.println("不存在该学生！");
        }
    }

    public static void studentClassSearch() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查询的学生学历");
        String studentClass = scanner.nextLine();
        boolean exist = false;
        int index = 1;
        for (Student student : students) {
            if (student.getStudentClass().equals(studentClass)) {
                System.out.println(String.format("%-5d | %s", index++, student.toString()));
                exist = true;
            }
        }
        if (!exist) {
            System.out.println("不存在该学生！");
        }
    }

    public static void deductPoints() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要扣分的学生学号");
        String studentID = scanner.nextLine();
        boolean exist = false;
        int index = 1;
        for (Student student : students) {
            if (student.getStudentID().equals(studentID)) {
                System.out.println("请输入该学员本次所扣分数：");
                int score = scanner.nextInt();
                student.setViolationNum(student.getViolationNum() + 1);
                student.setViolationScore(student.getViolationScore() + score);
                System.out.println("学员：" + student.getStudentName() + student.getStudentID() + "本次扣分：" + score + "分，累计扣分次数：" + student.getViolationNum() + "次，累计扣分：" + student.getViolationScore());
                exist = true;
            }
        }
        if (!exist) {
            System.out.println("不存在该学生！");
        }
    }

    public static void systemInfoManage() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==系统信息管理==");
            System.out.println("1.部门管理");
            System.out.println("2.员工管理");
            System.out.println("3.返回上一级菜单");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    departmentManage();
                    break;
                case 2:
                    employeeManager();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("没有这个选项，请重新输入！");
            }
        }
    }

    public static void departmentManage() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==部门管理==");
            System.out.println("1.新增部门");
            System.out.println("2.修改部门");
            System.out.println("3.删除部门");
            System.out.println("4.查看部门");
            System.out.println("5.返回上一级菜单");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    addDepartMent();
                    break;
                case 2:
                    modifyDepartMent();
                    break;
                case 3:
                    deleteDepartMent();
                    break;
                case 4:
                    viewDepartMent();
                    break;
                case 5:
                    System.out.println("返回中。。。");
                    return;
                default:
                    System.out.println("没有这个选项，请重新输入！");
            }
        }
    }

    public static void addDepartMent() {
        Scanner scanner = new Scanner(System.in);
        String departMentName = null;
        String departmentJob = null;

        while (true) {
            System.out.println("输入新增部门名称：");
            departMentName = scanner.nextLine();
            Boolean isRepeat = false;
            for (int i = 0; i < departments.size(); i++) {
                if (departMentName.equals(departments.get(i).getName())) {
                    System.out.println("部门名称重复！");
                    isRepeat = true;
                }
            }
            if (!isRepeat) {
                break;
            }
        }
        System.out.println("输入新增部门职务：");
        departmentJob = scanner.nextLine();

        Department department = new Department(departMentName, departmentJob);
        departments.add(department);
        System.out.println("部门添加成功");
    }

    public static void modifyDepartMent() {
        Scanner scanner = new Scanner(System.in);
        String departMentName = null;
        String newDepartMentName = null;
        String departmentJob = null;
        int oldDepartMentID = -1;

        System.out.println("输入修改的部门名称：");
        departMentName = scanner.nextLine();


        boolean exist = false;
        for (int i = 0; i < departments.size(); i++) {
            if (departMentName.equals(departments.get(i).getName())) {
                oldDepartMentID = i;
                exist = true;
            }
        }
        if (!exist) {
            System.out.println(departMentName + " 部门名称不存在");
            return;
        }
        System.out.println("输入修改后的部门名称：");
        newDepartMentName = scanner.nextLine();
        System.out.println("输入修改后的部门职位：");
        departmentJob = scanner.nextLine();
        Department newDepartment = new Department(newDepartMentName, departmentJob);
        departments.set(oldDepartMentID, newDepartment);
        System.out.println("修改成功");
    }

    public static void deleteDepartMent() {
        Scanner scanner = new Scanner(System.in);
        int input = 0;
        while (true) {
            System.out.println("请输入删除的部门名称：");
            String departMentName = scanner.nextLine();
            int departMentNum = 0;
            boolean exist = false;
            for (int i = 0; i < departments.size(); i++) {
                if (departments.get(i).getName().equals(departMentName)) {
                    departMentNum = i;
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("不存在该部门名称！");
                return;
            }
            exist = false;
            for (Employee employee : employees) {
                if (employee.getDepartment().equals(departments.get(departMentNum).getName())) {
                    exist = true;
                }
            }
            if (exist) {
                System.out.println("该部门下存在员工，无法删除！");
                return;
            }
            while (true) {
                System.out.println("确认删除部门" + departMentName + "吗？1-确定 2-取消");
                try {
                    input = scanner.nextInt();
                } catch (Exception e) {
                    System.out.println("输入格式错误！");
                    continue;
                }
                if (input == 1) {
                    departments.remove(departMentNum);
                    System.out.println(departMentName + " 删除成功。");
                    return;
                } else if (input == 2) {
                    System.out.println("取消删除。");
                    return;
                } else {
                    System.out.println("输入错误！请重新输入！");
                }

            }
        }
    }

    public static void viewDepartMent() throws ParseException {
        int departMentNameWidth = 10;
        int departMentJobWidth = 8;

        // 格式化输出表头
        System.out.printf("%-" + departMentNameWidth + "s | %-" + departMentJobWidth + "s%n",
                "部门名称", "部门职位");


        // 格式化输出数据
        for (Department dpm : departments) {
            System.out.printf("%-" + departMentNameWidth + "s | %-" + departMentJobWidth + "s%n",
                    dpm.getName(), dpm.getJob());
        }

    }

    public static void employeeManager() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("=员工管理=");
            System.out.println("1.新增员工");
            System.out.println("2.修改员工");
            System.out.println("3.删除员工");
            System.out.println("4.查看员工");
            System.out.println("5.返回上一级菜单");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    addEmployee();
                    break;
                case 2:
                    modifyEmployee();
                    break;
                case 3:
                    deleteEmployee();
                    break;
                case 4:
                    viewEmployee();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("没有该选项，请重新输入！");
            }
        }
    }

    public static void addEmployee() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String start = null;
        Date startDate = null;

        while (true) {
            System.out.println("请输入新增员工用户名");
            String employeeName = scanner.nextLine();
            for (Employee emp : employees) {
                if (emp.getName().equals(employeeName)) {
                    exist = true;
                }
            }
            if (exist) {
                System.out.println("用户名已存在，请重新输入！");
                continue;
            }

            System.out.println("请输入新增员工姓名");
            String employeeID = scanner.nextLine();
            exist = false;
            for (Employee employee : employees) {
                if (employee.getName().equals(employeeID)) {

                    exist = true;
                }
            }
            if (exist) {
                System.out.println("员工姓名已存在，自动在姓名后加上1");
                employeeID += 1;
            }
            System.out.println("请输入新员工性别");
            String employeeGender = scanner.nextLine();

            while (true) {
                System.out.println("请输入新增员工所属部门");
                System.out.println("现有部门如下：");
                for (Department department : departments) {
                    System.out.println(department.getName());
                }
                exist = false;
                String employeeDepartment = scanner.nextLine();
                for (Department department : departments) {
                    if (department.getName().equals(employeeDepartment)) {
                        exist = true;
                    }
                }
                if (!exist) {
                    System.out.println("没有此部门！请重新选择！");
                    continue;
                }

                while (true) {
                    System.out.println("请输入新增员工入职时间(中间用-表示)");
                    start = scanner.nextLine();
                    try {
                        startDate = sdf.parse(start);
                        break;
                    } catch (ParseException e) {
                        System.out.println("日期格式不对！请重新输入！");
                    }
                }
                exist = false;
                while (true) {
                    System.out.println("请输入新增员工职位");
                    System.out.println("该部门职位如下：");
                    for (Department department : departments) {
                        if (department.getName().equals(employeeDepartment)) {
                            System.out.println(department.getJob());
                            exist = true;
                        }
                    }
                    if (!exist) {
                        System.out.println("该部门没有职位！");
                    }
                    exist = false;
                    String employeeJob = scanner.nextLine();
                    for (Department department : departments) {
                        if (department.getJob().equals(employeeJob)) {
                            exist = true;
                        }
                    }
                    if (!exist) {
                        System.out.println("该部门没有此职位！请重新选择！");
                        continue;
                    }
                    Employee employee = new Employee(employeeName, employeeID, employeeGender, employeeDepartment, startDate, employeeJob);
                    employees.add(employee);
                    System.out.println("新增员工：" + employeeName + "成功，职务：" + employeeJob + "，所属部门：" + employeeDepartment);
                    return;
                }
            }
        }
    }

    public static void modifyEmployee() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String start = null;
        Date startDate = null;

        while (true) {
            System.out.println("请输入修改员工用户名");
            String employeeName = scanner.nextLine();
            for (Employee emp : employees) {
                if (emp.getUsername().equals(employeeName)) {
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("用户名不存在！请重新输入！");
                continue;
            }

            System.out.println("请输入修改员工姓名");
            String employeeID = scanner.nextLine();
            exist = false;
            for (Employee employee : employees) {
                if (employee.getName().equals(employeeID)) {

                    exist = true;
                }
            }
            if (exist) {
                System.out.println("员工姓名已存在，自动在姓名后加上1");
                employeeID += 1;
            }
            System.out.println("请输入修改后员工性别");
            String employeeGender = scanner.nextLine();

            while (true) {
                System.out.println("请输入修改后员工所属部门");
                System.out.println("现有部门如下：");
                for (Department department : departments) {
                    System.out.println(department.getName());
                }
                exist = false;
                String employeeDepartment = scanner.nextLine();
                for (Department department : departments) {
                    if (department.getName().equals(employeeDepartment)) {
                        exist = true;
                    }
                }
                if (!exist) {
                    System.out.println("没有此部门！请重新选择！");
                    continue;
                }

                while (true) {
                    System.out.println("请输入修改后员工入职时间(中间用-表示)");
                    start = scanner.nextLine();
                    try {
                        startDate = sdf.parse(start);
                        break;
                    } catch (ParseException e) {
                        System.out.println("日期格式不对！请重新输入！");
                    }
                }
                exist = false;
                while (true) {
                    System.out.println("请输入修改后职位");
                    System.out.println("该部门职位如下：");
                    for (Department department : departments) {
                        if (department.getName().equals(employeeDepartment)) {
                            System.out.println(department.getJob());
                            exist = true;
                        }
                    }
                    if (!exist) {
                        System.out.println("该部门没有职位！");
                    }
                    exist = false;
                    String employeeJob = scanner.nextLine();
                    for (Department department : departments) {
                        if (department.getJob().equals(employeeJob)) {
                            exist = true;
                        }
                    }
                    if (!exist) {
                        System.out.println("该部门没有此职位！请重新选择！");
                        continue;
                    }
                    for (Employee emp : employees) {
                        if (emp.getName().equals(employeeName)) {
                            employees.remove(emp);
                            break;
                        }
                    }
                    Employee employee = new Employee(employeeName, employeeID, employeeGender, employeeDepartment, startDate, employeeJob);
                    employees.add(employee);
                    System.out.println("修改员工：" + employeeName + "成功，职务：" + employeeJob + "，所属部门：" + employeeDepartment);
                    return;
                }
            }
        }
    }


    public static void deleteEmployee() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        System.out.println("请输入删除员工用户名");
        String employeeName = scanner.nextLine();
        for (Employee emp : employees) {
            if (emp.getUsername().equals(employeeName)) {
                exist = true;
            }
        }
        if (!exist) {
            System.out.println("用户名不存在！请重新输入！");
            return;
        }
        for (Employee employee : employees) {
            if (employee.getUsername().equals(employeeName)) {
                while (true) {
                    System.out.println("请确认是否删除：1-确定 2-取消");
                    int choice = 0;
                    try {
                        choice = scanner.nextInt();
                    } catch (Exception e) {
                        System.out.println("输入类型错误，请重新输入！");
                        scanner.next();
                        continue;
                    }
                    switch (choice) {
                        case 1:
                            employees.remove(employee);
                            System.out.println("删除成功");
                            return;
                        case 2:
                            System.out.println("取消删除");
                            return;
                        default:
                            System.out.println("没有该选项！请重新输入！");
                    }

                }
            }
        }

    }


    public static void viewEmployee() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1.查看所有员工");
            System.out.println("2.员工名称查询");
            System.out.println("3.性别查询");
            System.out.println("4.入职时间查询");
            System.out.println("5.返回上一级菜单");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    ListAllEmployee();
                    break;
                case 2:
                    EmployeeNameSearch();
                    break;
                case 3:
                    EmployeeGenderSearch();
                    break;
                case 4:
                    EmployeeDateSearch();
                    break;
                case 5:
                    return;
            }
        }
    }

    public static void ListAllEmployee() {
        System.out.println(String.format("%-5s | %-10s | %-10s | %-10s | %-10s | %-20s | %-10s",
                "序号", "用户名", "姓名", "性别", "职位", "服务日期", "部门"));

        // 打印分割线
        System.out.println("-------------------------------------------------------------------------------------------------");

        // 打印员工信息
        int index = 1;
        for (Employee employee : employees) {
            System.out.println(String.format("%-5d | %s", index++, employee.toString()));
        }

    }

    public static void EmployeeNameSearch() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        while (true) {
            System.out.println("请输入需要查找的员工名称：");
            String employeeName = scanner.nextLine();
            for (Employee employee : employees) {
                if (employee.getUsername().equals(employeeName)) {
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("没有此用户，请重新输入！");
                continue;
            }
            System.out.println(String.format("%-5s | %-10s | %-10s | %-10s | %-10s | %-20s | %-10s",
                    "序号", "用户名", "姓名", "性别", "职位", "服务日期", "部门"));

            // 打印分割线
            System.out.println("-------------------------------------------------------------------------------------------------");

            // 打印员工信息
            int index = 1;
            for (Employee employee : employees) {
                if (employee.getName().equals(employeeName)) {
                    System.out.println(String.format("%-5d | %s", index++, employee.toString()));
                }

            }
            return;
        }
    }

    public static void EmployeeGenderSearch() {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        while (true) {
            System.out.println("请输入需要查找的性别：");
            String employeeGender = scanner.nextLine();
            for (Employee employee : employees) {
                if (employee.getGender().equals(employeeGender)) {
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("没有此用户，请重新输入！");
                continue;
            }
            System.out.println(String.format("%-5s | %-10s | %-10s | %-10s | %-10s | %-20s | %-10s",
                    "序号", "用户名", "姓名", "性别", "职位", "服务日期", "部门"));

            // 打印分割线
            System.out.println("-------------------------------------------------------------------------------------------------");

            // 打印员工信息
            int index = 1;
            for (Employee employee : employees) {
                if (employee.getGender().equals(employeeGender)) {
                    System.out.println(String.format("%-5d | %s", index++, employee.toString()));
                }

            }
            return;
        }

    }

    public static void EmployeeDateSearch() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        boolean exist = false;
        while (true) {
            System.out.println("请输入需要查找的入职时间范围（中间用~分割！）：");
            String employeeDate = scanner.nextLine();
            String[] employeeDates = employeeDate.split("~");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = null;
            Date endDate = null;
            startDate = sdf.parse(employeeDates[0].trim());
            endDate = sdf.parse(employeeDates[1].trim());
            for (Employee employee : employees) {
                if (employee.getServiceDate().after(startDate) && employee.getServiceDate().before(endDate)) {
                    exist = true;
                }
            }
            if (!exist) {
                System.out.println("未找到范围在此日期间的员工！");
                continue;
            }
            System.out.println(String.format("%-5s | %-10s | %-10s | %-10s | %-10s | %-20s | %-10s",
                    "序号", "用户名", "姓名", "性别", "职位", "服务日期", "部门"));

            // 打印分割线
            System.out.println("-------------------------------------------------------------------------------------------------");

            // 打印员工信息
            int index = 1;
            for (Employee employee : employees) {
                if (employee.getServiceDate().after(startDate) && employee.getServiceDate().before(endDate)) {
                    System.out.println(String.format("%-5d | %s", index++, employee.toString()));
                }

            }
            return;
        }


    }

    public static void employeStatistics() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==数据统计管理==");
            System.out.println("1.员工性别统计");
            System.out.println("2.员工职位统计");
            System.out.println("3.返回上一级菜单");
            System.out.println("请输入你的选择");
            int choice = 0;
            try {
                choice = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("输入类型错误，请重新输入！");
                scanner.next();
                continue;
            }
            switch (choice) {
                case 1:
                    employeeGenderStatistics();
                    break;
                case 2:
                    employeeJobStatistics();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("没有这个选项，请重新输入！");
            }
        }
    }

    public static void employeeGenderStatistics() {
        int count = 0;
        int male = 0;
        int female = 0;
        for (Employee employee : employees) {
            if (employee.getGender().equals("男")) {
                male++;
            } else if (employee.getGender().equals("女")) {
                female++;
            }
            count++;
        }
        double malePercentage = (double) male / count * 100;
        double femalePercentage = (double) female / count * 100;
        System.out.println("员工共计" + count + "人");
        System.out.println("男性占比:" + malePercentage + "%");
        System.out.println("女性占比" + femalePercentage + "%");
    }

    public static void employeeJobStatistics() {
        int count = 0;
        HashMap<String, Integer> jobs = new HashMap<>();
        for (Employee employee : employees) {

            String job = employee.getJob();
            jobs.put(job, jobs.getOrDefault(job, 0) + 1);
            count++;
        }

        for (Map.Entry<String, Integer> entry : jobs.entrySet()) {
            double jobPercentage = (double) entry.getValue() / count * 100;
            System.out.println(entry.getKey() + "占比：" + jobPercentage + "%");
        }
    }

    public static void DatabaseConnection() throws Exception {
        Connection conn = null;
        PreparedStatement clsStmt = null;
        PreparedStatement stuStmt = null;
        PreparedStatement depStmt = null;
        PreparedStatement empStmt = null;
        ResultSet clsRs = null;
        ResultSet stuRs = null;
        ResultSet depRs = null;
        ResultSet empRs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/managersystem";
            String user = "root";
            String password = "1234";
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功！");
            //---------班级信息载入----------------
            String Clssql = "select * from tb_classes";
            clsStmt = conn.prepareStatement(Clssql);
            clsRs = clsStmt.executeQuery();
            while (clsRs.next()) {
                String className = clsRs.getString("className");
                int classRoom = clsRs.getInt("classRoom");
                Date startDate = clsRs.getDate("startDate");
                Date endDate = clsRs.getDate("endDate");
                String headTeacher = clsRs.getString("headTeacher");

                ClassRoom classRoomObj = new ClassRoom(className, classRoom, startDate, endDate, headTeacher);
                System.out.println("班级列表已载入!");
                classes.add(classRoomObj);

            }
            //---------部门信息导入----------------
            String depSql = "select * from tb_dept";
            depStmt = conn.prepareStatement(depSql);
            depRs = depStmt.executeQuery();
            while (depRs.next()) {
                String name = depRs.getString("name");
                String job = depRs.getString("job");
                Department depObj = new Department(name, job);
                System.out.println("部门列表已载入!");
                departments.add(depObj);
            }
            //---------学生信息导入----------------
            String stuSql = "select * from tb_stu";
            stuStmt = conn.prepareStatement(stuSql);
            stuRs = stuStmt.executeQuery();
            while (stuRs.next()) {
                String studentName = stuRs.getString("studentName");
                String studentID = stuRs.getString("studentID");
                String studentGender = stuRs.getString("studentGender");
                String studentPhoneNumber = stuRs.getString("studentPhoneNumber");
                String studentEdu = stuRs.getString("studentEdu");
                String studentClass = stuRs.getString("studentClass");
                int violationNum = stuRs.getInt("violationNum");
                int violationScore = stuRs.getInt("violationScore");
                Student student = new Student(studentName, studentID, studentGender, studentPhoneNumber, studentEdu, studentClass);
                System.out.println("学生列表已载入");
                students.add(student);
            }
            //---------员工信息导入----------------
            String empSql = "select * from tb_emp";
            empStmt = conn.prepareStatement(empSql);
            empRs = empStmt.executeQuery();
            while (empRs.next()) {
                String username = empRs.getString("username");
                String name = empRs.getString("name");
                String gender = empRs.getString("gender");
                String job = empRs.getString("job");
                Date ServiceDate = empRs.getDate("ServiceDate");
                String department = empRs.getString("department");
                Employee employee = new Employee(username, name, gender, job, ServiceDate, department);
                System.out.println("员工信息已载入！");
                employees.add(employee);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void DatabaseOutPut() throws Exception {
        Connection conn = null;
        PreparedStatement clsStmt = null;
        PreparedStatement clsCheckStmt = null;
        ResultSet clsRs = null;
        PreparedStatement stuStmt = null;
        PreparedStatement stuCheckStmt = null;
        ResultSet stuRs = null;
        PreparedStatement empStmt = null;
        PreparedStatement empCheckStmt = null;
        ResultSet empRs = null;
        PreparedStatement depStmt = null;
        PreparedStatement depCheckStmt = null;
        ResultSet depRs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/managersystem";
            String user = "root";
            String password = "1234";
            conn = DriverManager.getConnection(url, user, password);
            //-------班级导出---------
            String insertClsSql = "INSERT INTO tb_classes (className,classRoom,startDate,endDate,headTeacher) VALUES (?,?,?,?,?)";
            clsStmt = conn.prepareStatement(insertClsSql);

            String checkClsSql = "select COUNT(*) from tb_classes WHERE className = ?";
            clsCheckStmt = conn.prepareStatement(checkClsSql);
            for (ClassRoom classRoomObj : classes) {
                clsCheckStmt.setString(1, classRoomObj.getClassName());
                clsRs = clsCheckStmt.executeQuery();
                if (clsRs.next() && clsRs.getInt(1) == 0) {
                    clsStmt.setString(1, classRoomObj.getClassName());
                    clsStmt.setInt(2, classRoomObj.getClassRoom());
                    clsStmt.setDate(3, new java.sql.Date(classRoomObj.getStartDate().getTime()));
                    clsStmt.setDate(4, new java.sql.Date(classRoomObj.getEndDate().getTime()));
                    clsStmt.setString(5, classRoomObj.getHeadTeacher());
                    clsStmt.executeUpdate();
                    System.out.println("导入成功！");
                } else {
                    System.out.println("记录已存在，跳过导入！");
                }
            }
            System.out.println("班级数据导出成功！");
            //-------部门导出---------
            String insertDepSql = "INSERT INTO tb_dept (name,job) VALUES (?,?)";
            depStmt = conn.prepareStatement(insertDepSql);

            String checkDepSql = "select COUNT(*) from tb_dept WHERE name = ?";
            depCheckStmt = conn.prepareStatement(checkDepSql);
            for (Department departmentObj : departments) {
                depCheckStmt.setString(1, departmentObj.getName());
                depRs = depCheckStmt.executeQuery();
                if (depRs.next() && depRs.getInt(1) == 0) {
                    depStmt.setString(1, departmentObj.getName());
                    depStmt.setString(2, departmentObj.getJob());
                    depStmt.executeUpdate();
                    System.out.println("导入成功！");
                } else {
                    System.out.println("记录已存在，跳过导入！");
                }
            }
            System.out.println("部门数据导出成功！");
            //-------员工导出---------
            String insertEmpSql = "INSERT INTO tb_emp (username,name,gender,job,ServiceDate,department) VALUES (?,?,?,?,?,?)";
            empStmt = conn.prepareStatement(insertEmpSql);

            String checkEmpSql = "select COUNT(*) from tb_emp WHERE username = ?";
            empCheckStmt = conn.prepareStatement(checkEmpSql);
            for (Employee employeeObj : employees) {
                empCheckStmt.setString(1, employeeObj.getUsername());
                empRs = empCheckStmt.executeQuery();
                if (empRs.next() && empRs.getInt(1) == 0) {
                    empStmt.setString(1, employeeObj.getUsername());
                    empStmt.setString(2, employeeObj.getName());
                    empStmt.setString(3, employeeObj.getGender());
                    empStmt.setString(4, employeeObj.getJob());
                    empStmt.setDate(5, new java.sql.Date(employeeObj.getServiceDate().getTime()));
                    empStmt.setString(6, employeeObj.getDepartment());
                    empStmt.executeUpdate();
                    System.out.println("导入成功！");
                } else {
                    System.out.println("记录已存在，跳过导入！");
                }
            }
            System.out.println("员工数据导出成功！");
            //-------学生导出---------
            String insertStuSql = "INSERT INTO tb_stu (studentName,studentID,studentGender,studentPhoneNumber,studentEdu,studentClass,violationNum,violationScore) VALUES (?,?,?,?,?,?,?,?)";
            stuStmt = conn.prepareStatement(insertStuSql);

            String checkStuSql = "select COUNT(*) from tb_stu WHERE studentName = ?";
            stuCheckStmt = conn.prepareStatement(checkStuSql);
            for (Student studentObj : students) {
                stuCheckStmt.setString(1, studentObj.getStudentName());
                stuRs = stuCheckStmt.executeQuery();
                if (stuRs.next() && stuRs.getInt(1) == 0) {
                    stuStmt.setString(1, studentObj.getStudentName());
                    stuStmt.setString(2, studentObj.getStudentID());
                    stuStmt.setString(3, studentObj.getStudentGender());
                    stuStmt.setString(4, studentObj.getStudentPhoneNumber());
                    stuStmt.setString(5, studentObj.getStudentEdu());
                    stuStmt.setString(6, studentObj.getStudentClass());
                    stuStmt.setInt(7, studentObj.getViolationNum());
                    stuStmt.setInt(8, studentObj.getViolationScore());
                    stuStmt.executeUpdate();
                    System.out.println("导入成功！");
                } else {
                    System.out.println("记录已存在，跳过导入！");
                }
            }
            System.out.println("学生数据导出成功！");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (clsStmt != null) {
                try {
                    clsStmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (stuStmt != null) {
                    try {
                        stuStmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if (empStmt != null) {
                        try {
                            empStmt.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        if (depStmt != null) {
                            try {
                                depStmt.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        if (conn != null) {
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        if (clsRs != null) {

                            try {
                                clsRs.close();
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        if (stuRs != null) {

                            try {
                                stuRs.close();
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        if (empRs != null) {

                            try {
                                empRs.close();
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        if (depRs != null) {

                            try {
                                depRs.close();
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }

                        }
                    }
                }
            }
            System.out.println("???");
        }

    }
}

